package com.wswy.wzcx.widget.shadow;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import com.wswy.wzcx.R;

/**
 * ShadowLayout.java
 * https://github.com/lijiankun24/ShadowLayout
 * <p>
 * Created by lijiankun on 17/8/11.
 */

public class ShadowView extends View {

  public static final int ALL = 0x1111;

  public static final int LEFT = 0x0001;

  public static final int TOP = 0x0010;

  public static final int RIGHT = 0x0100;

  public static final int BOTTOM = 0x1000;

  public static final int SHAPE_RECTANGLE = 0x0001;

  public static final int SHAPE_OVAL = 0x0010;


  private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  private RectF mRectF = new RectF();

  /**
   * 阴影的颜色
   */
  private int mShadowColor = Color.TRANSPARENT;

  /**
   * 阴影的大小范围
   */
  private float mShadowRadius = 0;

  /**
   * 阴影 x 轴的偏移量
   */
  private float mShadowDx = 0;

  /**
   * 阴影 y 轴的偏移量
   */
  private float mShadowDy = 0;

  private float mEffect = 0;

  private float mRadius = 0f;

  /**
   * 阴影显示的边界
   */
  private int mShadowSide = ALL;

  /**
   * 阴影的形状，圆形/矩形
   */
  private int mShadowShape = SHAPE_RECTANGLE;

  public ShadowView(Context context) {
    this(context, null);
  }

  public ShadowView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public ShadowView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
  }

  /**
   * 读取设置的阴影的属性
   *
   * @param attrs 从其中获取设置的值
   */
  private void init(AttributeSet attrs) {

    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ShadowView);
    if (typedArray != null) {
      mShadowColor = typedArray.getColor(R.styleable.ShadowView_sl2_shadowColor,
          getContext().getResources().getColor(android.R.color.black));
      mShadowRadius = typedArray.getDimension(R.styleable.ShadowView_sl2_shadowRadius, dip2px(4));
      mShadowDx = typedArray.getDimension(R.styleable.ShadowView_sl2_shadowDx, 0);
      mShadowDy = typedArray.getDimension(R.styleable.ShadowView_sl2_shadowDy, 0);
      mEffect = typedArray.getDimension(R.styleable.ShadowView_sl2_effect, 0);
      mShadowSide = typedArray.getInt(R.styleable.ShadowView_sl2_shadowSide, ALL);
      mShadowShape = typedArray.getInt(R.styleable.ShadowView_sl2_shadowShape, SHAPE_RECTANGLE);
      mRadius = typedArray.getDimension(R.styleable.ShadowView_sl2_radius,0f);
      typedArray.recycle();
    }

    mPaint.reset();
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.TRANSPARENT);

    setUpShadowPaint();

    //https://developer.android.com/guide/topics/graphics/hardware-accel
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    float rectLeft = 0;
    float rectTop = 0;
    float rectRight = this.getMeasuredWidth();
    float rectBottom = this.getMeasuredHeight();
    int paddingLeft = 0;
    int paddingTop = 0;
    int paddingRight = 0;
    int paddingBottom = 0;
    if ((mShadowSide & LEFT) == LEFT) {
      rectLeft = mEffect;
      paddingLeft = (int) mEffect;
    }
    if ((mShadowSide & TOP) == TOP) {
      rectTop = mEffect;
      paddingTop = (int) mEffect;
    }
    if ((mShadowSide & RIGHT) == RIGHT) {
      rectRight = rectRight- mEffect;
      paddingRight = (int) mEffect;
    }
    if ((mShadowSide & BOTTOM) == BOTTOM) {
      rectBottom = rectBottom - mEffect;
      paddingBottom = (int) mEffect;
    }
    if (mShadowDy != 0.0f) {
      rectBottom = rectBottom - mShadowDy;
      paddingBottom = paddingBottom + (int) mShadowDy;
    }
    if (mShadowDx != 0.0f) {
      rectRight = rectRight - mShadowDx;
      paddingRight = paddingRight + (int) mShadowDx;
    }
    mRectF.left = rectLeft;
    mRectF.top = rectTop;
    mRectF.right = rectRight;
    mRectF.bottom = rectBottom;
    this.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }


  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mShadowShape == SHAPE_RECTANGLE) {
      if (mRadius > 0){

        canvas.drawRoundRect(mRectF,mRadius,mRadius,mPaint);

      }else {
        canvas.drawRect(mRectF, mPaint);
      }


    } else if (mShadowShape == SHAPE_OVAL) {
      canvas.drawCircle(mRectF.centerX(), mRectF.centerY(), Math.min(mRectF.width(), mRectF.height()) / 2, mPaint);
    }
  }

  public void setShadowColor(int shadowColor) {
    mShadowColor = shadowColor;
    setUpShadowPaint();
    postInvalidate();
  }

  public void setShadowRadius(float shadowRadius) {
    mShadowRadius = shadowRadius;
    setUpShadowPaint();
    requestLayout();
    postInvalidate();
  }



  private void setUpShadowPaint() {

    mPaint.setShadowLayer(mShadowRadius, mShadowDx, mShadowDy, mShadowColor);
  }

  /**
   * dip2px dp 值转 px 值
   *
   * @param dpValue dp 值
   * @return px 值
   */
  private float dip2px(float dpValue) {
    DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
    float scale = dm.density;
    return (dpValue * scale + 0.5F);
  }
}